using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.Events;

namespace LS.GameKit 
{
    public abstract class CommandSender : GameEventBehaviour
    {
        public float coolDown = 1;
        public CommandChannel sendChannel;
        public string eventKey;
        public CommandReceiver receiverObject;
        public UnityEvent<CommandType, object[]> CommandUnityEvent = new UnityEvent<CommandType, object[]>();

        public string onSendAudio;
        public float audioDelay = 0;

        private float lastSendTime;

        public float Temperature
        {
            get
            {
                return 1f - Mathf.Clamp01(Time.time - lastSendTime);
            }
        }

        public void Send(params object[] values)
        {
            if (!CanSend()) return;
            if (Time.time - lastSendTime < coolDown) return;
            lastSendTime = Time.time;
            if(sendChannel == CommandChannel.All || sendChannel == CommandChannel.ReceiverObject)
            {
                SendCommand(receiverObject, values);
            }

            if (sendChannel == CommandChannel.All || sendChannel == CommandChannel.EventModule)
            {
                SendCommandByEventModule(eventKey, values);
            }

            if (sendChannel == CommandChannel.All || sendChannel == CommandChannel.UnityEvent)
            {
                SendCommandByUnityEvent(CommandUnityEvent, values);
            }
        }

        protected abstract bool CanSend();
        protected abstract bool SendCommand(CommandReceiver receiver, params object[] values);
        protected abstract bool SendCommandByUnityEvent(UnityEvent<CommandType, object[]> unityEvent, params object[] values);
        protected abstract bool SendCommandByEventModule(string eventKey, params object[] values);

        protected virtual void Reset()
        {
            receiverObject = GetComponent<CommandReceiver>();
        }
    }
}
